package com.example.singleactivitytemplate.ui.base

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding



/**
 * 基础 Fragment
 *
 * @author Joymed
 */
abstract class BaseFragment<VB : ViewBinding, VM : BaseViewModel> : Fragment() {
    protected var viewBinding: VB? = null
    protected var mViewModel: VM? = null


    override fun onAttach(context: Context) {
        super.onAttach(context)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        viewBinding = getViewBindingClass(inflater, container)

        mViewModel = ViewModelProvider(this)[getViewModelClass()]


        return viewBinding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initOnListener()
        initObserver()
        initData()
    }

    override fun onStart() {
        super.onStart()

    }

    override fun onResume() {
        super.onResume()

    }

    override fun onPause() {
        super.onPause()

    }

    override fun onStop() {
        super.onStop()

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {

            //Fragment隐藏时调用
            //hidden();
        } else {

            //Fragment显示时调用
            //show();
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()

    }

    override fun onDetach() {
        super.onDetach()
        try {

            detach()
        } catch (e: Exception) {

        }
    }

    /**
     * 显示toast
     */
    fun toast(msg: String?) {

    }

    fun showInfo(msg: String) {

    }

    fun showInfo(msg: Int) {

    }

    fun showInfo(type: Int, msg: String) {


    }

    /**
     * 传入Bind对象
     */
    protected abstract fun getViewBindingClass(inflater: LayoutInflater, container: ViewGroup?): VB

    protected abstract fun getViewModelClass(): Class<VM>

    protected abstract fun initData()

    protected abstract fun initOnListener()

    protected abstract fun initObserver()

    /**
     * Fragment销毁时调用
     */
    protected abstract fun detach()
}